﻿using LeRenard.Core.Versioning;
using LeRenard.Libraries.PlatformInspection.InfoNodes;
using LeRenard.Libraries.PlatformInspection.Inspectors;
using LeRenard.Libraries.PlatformInspection.Inspectors.Environment;
using LeRenard.Libraries.PlatformInspection.Inspectors.Network;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LeRenard.Libraries.PlatformInspection.WinForms.ClientApplication
{
    public partial class Main : Form
    {
        public Main()
        {
            // Init
            InitializeComponent();

            // Title screen
            var assemblyVersion = new AssemblyVersion(Assembly.GetExecutingAssembly());            
            uxAppName.Text = "Platform Inspector";
            uxAppVersion.Text = assemblyVersion.ToString();
            this.Text = uxAppName.Text + " - [" + assemblyVersion.ToString() + "]";

            // Init Inspectors
            Reload();
        }

        private void Reload()
        {
            // Clear Screen
            uxInspectors.Nodes.Clear();

            // Get inspectors
            var inspectors = new List<IInspector>();
            inspectors.Add(new EnvironmentInspector());
            inspectors.Add(new NetworkInspector());

            // Iterate and populate
            foreach (var inspector in inspectors)
            {
                // Add inspector node
                TreeNode inspectorNode = new TreeNode(inspector.Name);
                uxInspectors.Nodes.Add(inspectorNode);

                // Populate inspector node
                Populate(inspectorNode, inspector.GetInformation().InnerInfoNodes);

                // Add possible errors
                if (inspector.Exceptions.Count() != 0)
                {
                    TreeNode inspectorErrorNode = new TreeNode("Inspection Errors");
                    inspectorNode.Nodes.Add(inspectorErrorNode);

                    foreach (var errorInfo in inspector.Exceptions)
                    {
                        inspectorErrorNode.Nodes.Add(
                                errorInfo.Location + ": '" + errorInfo.Exception.ToString() + "'"
                            );
                    }
                }
            }            
        }

        private void Populate(TreeNode parent, IEnumerable<InfoNode> infoNodes)
        {
            foreach (var infoNode in infoNodes)
            {
                var treeNode = new TreeNode();
                if (infoNode.DisplayStrategy != InfoNodeEmptyValueDisplayStrategy.TreatAsNormalInfoNode)
                    treeNode.Text = infoNode.Name;
                else
                    treeNode.Text = infoNode.Name + ": '" + infoNode.Value + "'";

                parent.Nodes.Add(treeNode);

                if (infoNode.HasInnerInfoNodes)
                    Populate(treeNode, infoNode.InnerInfoNodes);
            }
        }

        private void uxReload_Click(object sender, EventArgs e)
        {
            Reload();
        }
    }
}
